import {ajax} from 'nc-lightapp-front';
import requestApi from '../requestApi'
import presetVar from '../presetVar';
import searchClick from './searchClick';
(function (){
    requestApi.tpl({
        async:false,
        data:"",
        success: (datavalue) => {
            window.defalutvalue = datavalue.data;
        }
    });
}());
export default function (props) {

   let  appcode = (props.getUrlParam("appcode")==undefined||props.getUrlParam("appcode")=='')?props.getSearchParam("c"):props.getUrlParam("appcode");
   let  pagecode =  (props.getUrlParam("pagecode")==undefined||props.getUrlParam("pagecode")=='')?props.getSearchParam("p"):props.getUrlParam("pagecode");
   presetVar.appcode=appcode;
   presetVar.pagecode2=pagecode;
    props.createUIDom(
        {
            appcode:appcode,
            pagecode:pagecode
        },
        (data) => {

            let metas = data.template;
            let tempArr = [];
            let searchitem = metas[presetVar.areaCode.searchArea];
            let headmeta = metas[presetVar.areaCode.billsArea];
            let bodymeta = metas[presetVar.areaCode.bodysArea];
            // console.log('数据====',appcode,pagecode,presetVar.areaCode.searchArea,metas,searchitem)

            searchitem.items.forEach((item) => {

                if(item.itemtype == 'refer'){
                    item.isShowUnit=true;
                }
                if(item.attrcode && item.attrcode=="pk_org" && window.defalutvalue && window.defalutvalue.pk_org){
                    item.initialvalue = {value:window.defalutvalue.pk_org,display:window.defalutvalue.pk_org_value}
                }
                if(item.attrcode && item.attrcode=="pk_currtype" && window.defalutvalue && window.defalutvalue.bzbm){
                    item.initialvalue = {value:window.defalutvalue.bzbm,display:window.defalutvalue.bzbm_value}
                }

                if (item.attrcode && item.attrcode === 'billmaker' && window.defalutvalue && window.defalutvalue.pk_psndoc) {
                    item.initialvalue = { value: window.defalutvalue.pk_psndoc, display: window.defalutvalue.pk_psndoc_value };
                    item.queryCondition = () => {
                        return {
                            GridRefActionExt: 'nccloud.web.erm.action.ref.sqlbuilder.PsndocTreeGridRef'
                        }; 
                    };
                }
                tempArr.push(item);
             });
            let transtype = "";
            
            if(data.context.paramMap && data.context.paramMap.transtype){
                transtype = data.context.paramMap.transtype
            };
            if(data.context.paramMap && data.context.paramMap.pagecode){
                window.presetVar.topagecode = data.context.paramMap.pagecode;
            }

            window.presetVar.tradetype = props.getUrlParam('tradetype') || transtype;
            
            let meta = {
                ...metas,
                searchArea: {
                    moduletype: 'search',
                    items : tempArr
                },
                head:{
                    moduletype: 'table',
                    items: headmeta.items
                },
                body:{
                    moduletype: 'table',
                    items: bodymeta.items
                }
            }

            //add 无金额申请meta, 后端无法修改元数据,只能前端加
            addToSearchMeta(searchitem.items, props)
            props.meta.setMeta(meta);
            searchClick.call(this,props,null, 'defaultSearch');
    });

}

function addToSearchMeta(searchItems, props) {
    let multiLang = props.MutiInit.getIntl(2011);
    const noAmountItemMeta = {
        attrcode: "noamount",
        checkStrictly: false,
        col: "3",
        colnum: "1",
        containlower: false,
        datatype: "32",
        disabled: false,
        editAfterFlag: false,
        fieldDisplayed: "refcode",
        fieldValued: "refpk",
        hyperlinkflag: false,
        isDataPowerEnable: false,
        isResLabel: false,
        isShowUnit: false,
        isdrag: true,
        isfixedcondition: false,
        isnextrow: false,
        isnotmeta: false,
        itemtype: "switch",
        label: multiLang && multiLang.get('201102SQ_C-0001'),
        leftspace: "0",
        maxlength: "1",
        onlyLeafCanSelect: false,
        options: [{display: "", value: ""}, {display: "是", value: "Y"}, {display: "否", value: "N"}],
        position: "51",
        queryOperateType: "=@",
        required: false,
        rightspace: "0",
        rows: "0",
        scale: "0",
        usefunc: false,
        visible: true
    }
    searchItems.push(noAmountItemMeta)
}